package views.features;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.GridLayout;
import java.awt.Image;
import java.awt.Insets;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.io.File;
import java.io.IOException;

import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.SwingConstants;

import views.util.GameColors;
import core.GameLauncher;

public class GameMenuView extends JPanel {

	private JPanel gameList;
	private Font font = new Font("Segoe UI", Font.PLAIN, 20);
	private GameLauncher gameFrame;
	private BlockPanel block1;
	private BlockPanel block2;
	private BlockPanel block3;
	private BlockPanel block4;
	private BlockPanel block5;
	private GridBagConstraints gbc = new GridBagConstraints();
	private JPanel brianPanel;
	private JLabel brianLabel;
	private static GameMenuView instance;

	public static GameMenuView newInstance(GameLauncher gameFrame)
			throws IOException {
		if (instance == null)
			instance = new GameMenuView(gameFrame);
		return instance;
	}

	private GameMenuView(GameLauncher gameFrame) throws IOException {
		this.gameFrame = gameFrame;

		// setLayout(new GridLayout(3, 1, 0, 0));
		setBackground(GameColors.GRIGIO_CHIARO);
		setLayout(new BorderLayout());
		brianPanel = new JPanel() {
			public void paintComponent(Graphics g) {
				super.paintComponent(g);
				setOpaque(false);
				try {
					Image img = ImageIO.read(new File(
							"resources\\gameMenu\\brianBrain.png"));
					g.drawImage(img, 0, 0, null);
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		};
		brianPanel.setPreferredSize(new Dimension(800, 260));
		brianPanel.repaint();
		brianLabel = new JLabel();
		brianPanel.setBackground(GameColors.GRIGIO_CHIARO);
		brianPanel.add(brianLabel, BorderLayout.SOUTH);
		brianLabel.setFont(font);
		brianLabel.setForeground(Color.BLACK);
		brianLabel.setPreferredSize(new Dimension(800, 260));
		brianLabel.setHorizontalAlignment(SwingConstants.CENTER);
		brianLabel.setVerticalAlignment(SwingConstants.CENTER);

		add(brianPanel, BorderLayout.NORTH);
		JPanel main = new JPanel(new GridBagLayout());
		main.setBackground(GameColors.GRIGIO_CHIARO);
		add(main, BorderLayout.CENTER);
		gbc.gridx = 0;
		gbc.gridy = 0;
		gbc.insets = new Insets(-20, 5, 25, 5);
		JPanel panel1 = new JPanel();
		panel1.setBackground(GameColors.GRIGIO_CHIARO);
		main.add(panel1, gbc);
		panel1.setLayout(new GridBagLayout());

		GridBagConstraints gbc2 = new GridBagConstraints(); // gbc interno
		gbc2.insets = new Insets(5, 20, 5, 20);
		gbc2.gridx = 1;
		// Image icon11 = ImageIO.read(new File(
		// "resources\\gameMenu\\confrontoRapido.png"));
		// Image icon12 = ImageIO.read(new File(
		// "resources\\gameMenu\\schiacciaArancia.png"));

		block1 = new BlockPanel("Velocità", GameColors.AZZURRO,
				"resources\\gameMenu\\confrontoRapido",
				"resources\\gameMenu\\schiacciaArancia", "Confronto Rapido",
				"Schiaccia Arancia");
		panel1.add(block1, gbc2);

		gbc2.gridx = 3;
		// Image icon21 = ImageIO.read(new File(
		// "resources\\gameMenu\\memorizzaTabella.png"));
		// Image icon22 = ImageIO.read(new File(
		// "resources\\gameMenu\\flipperControllato.png"));

		block2 = new BlockPanel("Memoria", GameColors.VERDE,
				"resources\\gameMenu\\memorizzaTabella",
				"resources\\gameMenu\\luceRiflessa", "Memorizza Tabella",
				"Luce Riflessa");
		panel1.add(block2, gbc2);

		gbc2.gridx = 5;
		// Image icon31 = ImageIO.read(new File(
		// "resources\\gameMenu\\pesciAffamati.png"));
		// Image icon32 = ImageIO.read(new File(
		// "resources\\gameMenu\\deviazioneNavi.png"));

		block3 = new BlockPanel("Attenzione", GameColors.ARANCIONE,
				"resources\\gameMenu\\pesciAffamati",
				"resources\\gameMenu\\svegliaTalpa", "Pesci Disorientati",
				"Sveglia la Talpa");
		panel1.add(block3, gbc2);

		JPanel panel2 = new JPanel();
		panel2.setLayout(new GridBagLayout());
		panel2.setBackground(GameColors.GRIGIO_CHIARO);
		gbc.gridy = 2;
		main.add(panel2, gbc);

		gbc2.gridx = 1;
		// Image icon41 = ImageIO.read(new File(
		// "resources\\gameMenu\\coloriEParole.png"));
		// Image icon42 = ImageIO.read(new File(
		// "resources\\gameMenu\\gocceDiParole.png"));

		block4 = new BlockPanel("Flessibilità", GameColors.BORDEAUX,
				"resources\\gameMenu\\coloriEParole",
				"resources\\gameMenu\\gocceDiParole", "Colori e Parole",
				"Gocce di Parole");
		panel2.add(block4, gbc2);

		gbc2.gridx = 3;
		// Image icon51 = ImageIO.read(new File(
		// "resources\\gameMenu\\memoriaOrdinata.png"));
		// Image icon52 = ImageIO.read(new File(
		// "resources\\gameMenu\\pallonciniMatematici.png"));

		block5 = new BlockPanel("Problem Solving", GameColors.VIOLA,
				"resources\\gameMenu\\memoriaOrdinata",
				"resources\\gameMenu\\pallonciniMatematici",
				"Memoria Ordinata", "Palloni Matematici");
		panel2.add(block5, gbc2);

	}

	public void addMouseListenerToGames(MouseListener ml) {
		block1.addMouseListenerToLabels(ml);
		block2.addMouseListenerToLabels(ml);
		block3.addMouseListenerToLabels(ml);
		block4.addMouseListenerToLabels(ml);
		block5.addMouseListenerToLabels(ml);
	}

	public void setBrianBrainSpeech(String s) {
		brianLabel.setText("" + s);
	}

	private class BlockPanel extends JPanel {

		private JLabel game1;
		private JLabel game2;

		public BlockPanel(String sectionTitle, Color sectionColor,
				String icon1Path, String icon2Path, String title1, String title2) {
			this.setLayout(new BorderLayout());
			this.setOpaque(false);
			Dimension dim = new Dimension(220, 145);
			this.setPreferredSize(dim);
			JLabel title = new JLabel();
			title.setHorizontalAlignment(JLabel.CENTER);
			title.setFont(font);
			Dimension titleDim = new Dimension(220, 35);
			title.setPreferredSize(titleDim);
			title.setBackground(sectionColor);
			title.setOpaque(true);
			title.setForeground(Color.WHITE);
			title.setText(sectionTitle);
			JPanel gamesPanel = new JPanel();
			gamesPanel.setOpaque(false);
			gamesPanel.setLayout(new GridLayout(1, 2, 2, 1));

			game1 = new IconLabel(title1, icon1Path, sectionColor);
			game2 = new IconLabel(title2, icon2Path, sectionColor);
			MouseListener listener = new IconMouseListener();
			game1.addMouseListener(listener);
			game2.addMouseListener(listener);

			gamesPanel.add(game1);
			gamesPanel.add(game2);
			add(title, BorderLayout.NORTH);
			add(gamesPanel, BorderLayout.CENTER);

		}

		public void addMouseListenerToLabels(MouseListener ml) {
			game1.addMouseListener(ml);
			game2.addMouseListener(ml);
		}

		private class IconLabel extends JLabel {
			private ImageIcon icon;
			private ImageIcon iconOver;
			private Color color;
			Font gamefont = new Font("Segoe UI", Font.PLAIN, 12);

			public IconLabel(String title, String pathWithoutExtension,
					Color color) {
				super(new ImageIcon(pathWithoutExtension + ".png"));
				icon = new ImageIcon(pathWithoutExtension + ".png");
				iconOver = new ImageIcon(pathWithoutExtension + "Over.png");
				this.color = color;
				this.setName(title);
				this.setFont(gamefont);
				this.setHorizontalTextPosition(JLabel.CENTER);
				this.setVerticalTextPosition(JLabel.BOTTOM);
				this.setText(title);
				this.setForeground(color);
				this.setOpaque(true);
				this.setBackground(Color.WHITE);
			}

			public void enteredColors() {
				this.setBackground(color);
				this.setForeground(Color.WHITE);
				setIcon(iconOver);
			}

			public void extitedColors() {
				this.setBackground(Color.WHITE);
				this.setForeground(color);
				setIcon(icon);
			}

		}

		private class IconMouseListener extends MouseAdapter {

			@Override
			public void mouseClicked(MouseEvent e) {
				// TODO Auto-generated method stub

			}

			@Override
			public void mouseEntered(MouseEvent e) {
				IconLabel label = (IconLabel) e.getSource();
				label.enteredColors();
			}

			@Override
			public void mouseExited(MouseEvent e) {
				IconLabel label = (IconLabel) e.getSource();
				label.extitedColors();
			}

		}
	}
}
